package cn.zzuli.zuoye2;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn1;

        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick1(v);
            }
        });

    };
    @Override

    public void onBackPressed(){
        AlertDialog dialog;
        dialog = new AlertDialog.Builder(MainActivity.this).setTitle("确认")
                .setMessage("是否确认退出")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .setNegativeButton("取消",null)
                .create();
        dialog.show();
    }
    public void onClick1(View v){
        String user, pass;
        Post post=new Post();
        EditText editText=(EditText)findViewById(R.id.et_name);
        EditText editText1=(EditText)findViewById(R.id.et_password);
        user=editText.getText().toString();
        pass=editText1.getText().toString();
        if (user.equals("")||pass.equals(""))
        {
            Toast toast=Toast.makeText(this,null, Toast.LENGTH_LONG);
            toast.setText("请输入用户名或密码");
            toast.show();
        }else
        {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Post post = new Post();
                    String s = post.login(user, pass);
                    System.out.println(s);
                    try {
                        System.out.println(s);
                        JSONObject object = new JSONObject(s);
                        int errorCode = object.optInt("errorCode");
                        if (errorCode == 0) {
                            Uri uri = Uri.parse("https://www.wanandroid.com");
                            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                            startActivity(intent);
                            removeActivity(MainActivity.this);
                        } else {
                            Toast.makeText(MainActivity.this,"111", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    public  class Post{

        private String login(String name,String pwd){
            try {
                URL url = new URL("https://www.wanandroid.com/user/login");
                HttpURLConnection cn = (HttpURLConnection)url.openConnection();
                cn.setRequestMethod("POST");
                cn.setDoOutput(true);
                cn.setUseCaches(false);
                System.out.println(name);
                System.out.println(pwd);
                String str = "username="+ URLEncoder.encode(name,"utf-8")+"&password="+URLEncoder.encode(pwd,"utf-8");
                OutputStream outputStream = cn.getOutputStream();
                outputStream.write(str.getBytes());
                outputStream.flush();
                if(cn.getResponseCode()==200){
                    InputStream inputStream = cn.getInputStream();
                    String s = stream2String(inputStream);
                    return s;
                }
            }catch (Exception e){
                e.printStackTrace();
                return "error";
            }
            return null;
        }
    }
    private String stream2String(InputStream inputStream){
        BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        try{
            while((line = bf.readLine())!=null){
                stringBuffer.append(line);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
    public static void removeActivity(Activity activity){
        activity.finish();
    }

}